package com.qf.smartplatform.controller;/*
*王浩政 
*whz
2022/9/7
17:29
*小米商城
*/

import com.qf.smartplatform.dto.R;
import com.qf.smartplatform.dto.UserDto;
import com.qf.smartplatform.pojo.SysUserInfo;
import com.qf.smartplatform.service.UserService;
import com.qf.smartplatform.valigroup.Insert;
import com.qf.smartplatform.valigroup.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/users")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /*
    * 注册接口，接受用户传递的对象
    * */
    @PostMapping("/user")
    public R register(@Validated(Insert.class) @RequestBody UserDto userDto){
        System.err.println(userDto);
        userService.addUser(userDto);
        return R.OK();
    }

    @PostMapping("/login")
    public R login(@RequestBody @Validated(Select.class) UserDto userDto, HttpSession session, HttpServletRequest request){

        SysUserInfo sysUserInfo = userService.login(userDto.getUsername(), userDto.getPassword());
        session.setAttribute("user",sysUserInfo);

//        String remoteHost = request.getRemoteHost();
//        System.out.println("客户端的ip是：===>"+remoteHost);


        String remoteHost=request.getRemoteHost();
        System.out.println("客户端的ip是：===>"+remoteHost);
        return R.OK();
    }
}
